
*******************************************************************************
****				Appendix Table G.4 - MONTHLY FREQUENCY BY *********
****				TRAVEL TIME SAVINGS DECILE							*******
*******************************************************************************
 
use .\data\clean\I10W_laneuse_dataset_15nov14_wcensus, clear
capture drop if holiday==1
drop if dow==0 | dow==6
*WTP calculation
gen TT_dif_hr=dist/MLspeed-dist/ELspeed
gen WTP2=charged_toll/TT_dif_hr
drop if ELspeed==.
drop if TT_dif_hr==.
la var TT_dif_hr "Time in Hours"
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"
keep if hour>4 & hour<9
drop if TT_dif_hr<0
g decile=.
g TT_dif_min=TT_dif_hr*60
egen id=group(acct_no month)
egen TT_dif_hr_ma=mean(TT_dif_hr), by(id)
forval i= 10(10)90{
	qui centile TT_dif_hr, c(`i')
	replace decile=`i'/100 if TT_dif_hr<=`r(c_1)' & decile==.
	}
replace decile=1 if decile==.
replace decile=round(decile*10)

// Start by IDing accounts in data only one month
egen minmonth=min(month), by(acct_no)
egen maxmonth=max(month), by(acct_no)
drop if month==minmonth
drop minmonth
egen minmonth=min(month), by(acct_no)
g mfreq1moonly=minmonth==maxmonth
g count=1
egen freqpermo=sum(count), by(id)
egen avgfreqpermo=mean(freqpermo) if mfreq1moonly==0 , by(acct_no)
duplicates drop id,force 
replace avgfreqpermo=ceil(avgfreqpermo) if mfreq1moonly==0 
g mfreq1=avgfreqpermo==1
g mfreq2_5=inlist(avgfreqpermo,2,3,4,5)
g mfreq6_10=inlist(avgfreqpermo,6,7,8,9,10)
g mfreq11_20=cond(avgfreqpermo>10&freqpermo<21,1,0)
g mfreq20=cond(avgfreqpermo>20&freqpermo!=.,1,0)
duplicates drop acct_no,force 
qui ds mfreq*
local vlist "`r(varlist)'"
matrix A=J(10,7,.)
forval d=1/10{
	matrix A[`d',1]=`d'
	local i=2
	foreach v of local vlist{
		qui sum `v' if decile==`d'				// not same number for each decile because deciles are by trip, here is accounts
		local `v'_`d'=`r(sum)'
		local sum_`d'=`sum_`d''+``v'_`d''
		}
	foreach v of local vlist{
		local `v'_`d'_shr=100*round(``v'_`d''/`sum_`d'',.0001)
		matrix A[`d',`i']=``v'_`d'_shr'
		local i=`i'+1
		}
	}
putexcel set ".\results\appendix\AppendixFigsTabs.xlsx", modify sheet("Appendix Table G.4")
putexcel A1=("I") B1=("II") C1=("III") D1=("IV") E1=("V") F1=("VI") G1=("VII") 
putexcel A2=("Decile of Time Savings") B2=("One Month of Use") C2=("1x per Month") D2=("2-5x per Month") E2=("6-10x per Month") F2=("11-20x per Month") G2=(">20x per Month")
putexcel A3=matrix(A)
putexcel B3:G12, nfor(percent_d2)
